/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.sys>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
  * Name: ipc_types.h
  *
  * Purpose: ipc sysmunication base types for 'COM' Functional
  *
  * Developer:
  *   wen.gu , 2020-07-28
  *
  * TODO:
  *
  ***************************************************************************/

  /******************************************************************************
   **    INCLUDES
   ******************************************************************************/
#ifndef __PANDA_COM_RUNTIME_IPC_TYPES_H__
#define __PANDA_COM_RUNTIME_IPC_TYPES_H__
#include <memory>
#include <functional>
#include "panda/sys/sys_types.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

#define IPC_INVALID_ID (-1)
#define IPC_REQ_WAIT_INFINITE (-1)
#define MakePayloadPtr(size) std::shared_ptr<uint8_t>(new uint8_t[size], std::default_delete<uint8_t[]>())
/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace panda
{
namespace sys 
{

using  PayloadPtr = std::shared_ptr<uint8_t>;

struct IpcPacketData
{
    uint32_t size;
    PayloadPtr payload;
};


/** for client side */
/**for request async ,
 * parameter: [1]: method Name, [2]: response information data */
using IpcClientReplyHandler = std::function<void(const std::string&, const IpcPacketData&)>;

/** for process signal,
 * paramter: [1]: signal name, [2]: signal content */
using IpcClientSignalHandler = std::function<void(const std::string&, const IpcPacketData&)>;


/** for service side */
/** request handle, indicat who send the request */
class IpcServiceRequestHandle;

using IpcServiceRequestHandlePtr = std::shared_ptr<IpcServiceRequestHandle>;

/**
 * ServiceRequestHandle* [in] who request the service to do method.
 * const std::string& [in] method name
 * const IpcPacketData& [in] request parameter data 
 */
using IpcServiceMethodHandler = std::function<void(IpcServiceRequestHandlePtr, const std::string&, const IpcPacketData&)>;


/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/

namespace internal_impl
{
/**for check the base type of enum */
template<size_t sz = 1>
struct enum2Int 
{
    using type = uint8_t;
};

template<>
struct enum2Int<2>
{
    using type = uint16_t;
};

template<>
struct enum2Int<4>
{
    using type = uint32_t;
};

template<>
struct enum2Int<8>
{
    using type = uint64_t;
};

} /** namespace internal_impl */
} /** namespace sys */
} /** namespace panda */


#endif /** !__PANDA_COM_RUNTIME_IPC_TYPES_H__ */